package com.yuuko.yuukoapi.project.limb.controller

import com.yuuko.yuukoapi.common.request.PageParam
import com.yuuko.yuukoapi.common.response.AjaxResult
import com.yuuko.yuukoapi.common.response.None
import com.yuuko.yuukoapi.framework.extension.findPage
import com.yuuko.yuukoapi.project.limb.doman.entity.dto.DeviceFirmwareListSpec
import com.yuuko.yuukoapi.project.limb.doman.entity.dto.DeviceFirmwareUpdateInput
import com.yuuko.yuukoapi.project.limb.doman.entity.DeviceFirmware
import com.yuuko.yuukoapi.repository.limb.DeviceFirmwareRepository
import org.babyfish.jimmer.Page
import org.babyfish.jimmer.sql.ast.mutation.SaveMode
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/limb/device-firmware")
class DeviceFirmwareController(
    val deviceFirmwareRepository: DeviceFirmwareRepository
) {
    @GetMapping("/list")
    fun getDeviceFirmwareList(
        page: PageParam,
        spec: DeviceFirmwareListSpec
    ): AjaxResult<Page<DeviceFirmware>> {
        val result = deviceFirmwareRepository.findPage(page, spec)
        return AjaxResult.ok(result)
    }

    @PostMapping
    fun createDeviceFirmware(
        @RequestBody input: DeviceFirmwareUpdateInput
    ): AjaxResult<None?> {
        deviceFirmwareRepository.save(input, SaveMode.INSERT_ONLY)
        return AjaxResult.ok()
    }

    @PutMapping("/{id}")
    fun updateDeviceFirmware(
        @PathVariable id: String,
        @RequestBody input: DeviceFirmwareUpdateInput
    ): AjaxResult<None?> {
        deviceFirmwareRepository.save(input.toEntity { this.id = id }, SaveMode.UPDATE_ONLY)
        return AjaxResult.ok()
    }

    @DeleteMapping("/{id}")
    fun deleteDeviceFirmware(@PathVariable id: String): AjaxResult<None?> {
        deviceFirmwareRepository.deleteById(id)
        return AjaxResult.ok()
    }
}